<?php

//验证码相关的所有操作
class VCode
{
    private $charset = "abcdefghzkmnpqrstuvwxyABCDEFGHZKMNPQRSTUVWXY23456789"; //随机因子
    private $operator = ['+',"-","*"]; //随机因子
    private $code;     //验证码文字
    private $codelen = 4;    //验证码显示几个文字
    private $width = 115;   //验证码宽度
    private $height = 35;   //验证码高度
    private $img;       //验证码资源句柄
    private $font;     //指定的字体
    private $fontsize = 25;  //指定的字体大小
    private $fontcolor;     //字体颜色  随机
    private $line_num = 3; //图像中的干扰线
    private $snow_num = 3; //干扰雪花数
    private $type = 1; //类型

    //构造类  编写字体

    public function __construct()
    {
        $this->font = dirname(__FILE__) . '/font/elephant.ttf';
    }

    //创建4个随机码
    private function createCode()
    {
        if(rand(1,2) == 1){
            $this->type = 1;
            $_leng = strlen($this->charset) - 1;
            for ($i = 1; $i <= $this->codelen; $i++) {
                $this->code.= $this->charset[mt_rand(0, $_leng)];
            }
        }else{
            $this->type = 2;
            $rand = rand(1,3);
            if($rand == 1){
                $this->code =  rand(1,20) . '+' .rand(1,20) .'=';
            }elseif ($rand == 1){
                $this->code =  rand(10,20) . '-' .rand(0,9) .'=';
            }else{
                $this->code =  rand(1,9) . '*' .rand(1,9) .'=';
            }

        }

        return $this->code;
    }

    //创建背景
    private function createBg()
    {
        //创建画布 给一个资源jubing
        $this->img = imagecreatetruecolor($this->width, $this->height);
        //背景颜色
        //$color = imagecolorallocate($this->img, 221, 221, 221);
        $color = imagecolorallocate($this->img, rand(150,255),rand(150,255),rand(150,255));

        //画出一个矩形
        imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color);
    }

    //创建字体
    private function createFont()
    {
        $_x = (($this->width - 30) / $this->codelen);   //字体长度
        for ($i = 0; $i < $this->codelen; $i++) {
            //文字颜色
            $color = imagecolorallocate($this->img, rand(0,140), rand(0,140), rand(0,140));
            //资源句柄 字体大小 倾斜度 字体长度  字体高度  字体颜色  字体  具体文本
            imagettftext($this->img, $this->fontsize, 0, $_x * $i + 15, $this->height / 1.2, $color, $this->font, $this->code[$i]);
        }
    }

    //随机线条
    private function createLine()
    {
        //随机线条
        for ($i = 0; $i < $this->line_num; $i++) {
            $color = imagecolorallocate($this->img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
        }

//        //随机雪花
//        for ($i = 0; $i < $this->snow_num; $i++) {
//            $color = imagecolorallocate($this->img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
//            imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color);
//        }
        for($i=0;$i<200;$i++){
            //设置点的颜色，50-200颜色比数字浅，不干扰阅读
            $pointcolor = imagecolorallocate($this->img,rand(0,255), rand(0,255), rand(0,255));
            //imagesetpixel — 画一个单一像素
            imagesetpixel($this->img, rand(1,$this->width), rand(1,$this->height), $pointcolor);
        }
    }

    //输出背景
    public function outputImg()
    {
        //生成标头
        header('Content-Type:image/png');
        //输出图片
        imagepng($this->img);
        //销毁结果集
        imagedestroy($this->img);
    }

    //对外输出
    public function createImg()
    {
        //加载背景
        $this->createBg();
        //加载文件
        $this->createCode();
        //加载线条
        $this->createLine();
        //加载字体
        $this->createFont();
        //加载背景
        //$this->outPut();
    }

    //获取验证码
    public function getCode()
    {
        $this->code = rtrim($this->code,'=');
        return $this->type == 1 ? strtolower($this->code) : eval("return $this->code;");
    }
}

